package tsm.magic;

import java.util.List;

import org.apache.mina.core.buffer.IoBuffer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import tsm.constants.MagicConstant;
import tsm.util.SerializerHelper;

public class MagicGenerator {
	private static final Logger log = LoggerFactory.getLogger(MagicGenerator.class);

	// ============================PAGE============================

	/**
	 * @param scrollable
	 * @param layout
	 * @param row
	 *            for gird layout
	 * @param column
	 *            for gird layout
	 * @param title
	 * @param bgColor
	 *            TODO
	 * @param scrollVisible
	 *            TODO
	 * @param pageId
	 * @param stream
	 */
	public static void createPage(IoBuffer buffer, byte scrollable, byte layout, byte row, byte column, String title, int bgColor, byte scrollVisible, int pageId) {
		SerializerHelper.writeInt(MagicConstant.CREATE_PAGE, buffer);
		SerializerHelper.writeByte(scrollable, buffer);
		SerializerHelper.writeByte(layout, buffer);
		SerializerHelper.writeByte(row, buffer);
		SerializerHelper.writeByte(column, buffer);
		SerializerHelper.writeString(title, buffer);
		SerializerHelper.writeInt(bgColor, buffer);
		SerializerHelper.writeByte(scrollVisible, buffer);
		SerializerHelper.writeInt(pageId, buffer);
	}

	// ============================COMMANDS=============================
	public static void addCommandToPage(IoBuffer buffer, String commandText, byte offset, byte[] image, byte[] action) {
		SerializerHelper.writeInt(MagicConstant.ADD_COMMAND, buffer);
		SerializerHelper.writeString(commandText, buffer);
		SerializerHelper.writeByte(offset, buffer);
		SerializerHelper.write(image, buffer);
		SerializerHelper.write(action, buffer);
	}

	// ============================ACTION=============================
	public static void addActionToPage(IoBuffer buffer, byte oriented, byte[] action) {
		SerializerHelper.writeInt(MagicConstant.ADD_ACTION, buffer);
		SerializerHelper.writeByte(oriented, buffer);
		SerializerHelper.write(action, buffer);
	}

	// ============================COMPONENT============================
	public static void addContainerToPage(IoBuffer buffer, int containerId, byte constraints, byte scrollable, byte layout, byte row, byte column, byte topPadding,
			byte leftPadding, byte bottomPadding, byte rightPadding, byte topMargin, byte leftMargin, byte bottomMargin, byte rightMargin) {
		SerializerHelper.writeInt(MagicConstant.ADD_CONTAINER_TO_PAGE, buffer);
		SerializerHelper.writeByte(constraints, buffer);
		SerializerHelper.writeByte(scrollable, buffer);
		SerializerHelper.writeByte(layout, buffer);
		SerializerHelper.writeByte(row, buffer);
		SerializerHelper.writeByte(column, buffer);
		SerializerHelper.writeByte(topPadding, buffer);
		SerializerHelper.writeByte(leftPadding, buffer);
		SerializerHelper.writeByte(bottomPadding, buffer);
		SerializerHelper.writeByte(rightPadding, buffer);
		SerializerHelper.writeByte(topMargin, buffer);
		SerializerHelper.writeByte(leftMargin, buffer);
		SerializerHelper.writeByte(bottomMargin, buffer);
		SerializerHelper.writeByte(rightMargin, buffer);
		SerializerHelper.writeInt(containerId, buffer);
	}

	public static void addContainerToContainer(IoBuffer buffer, int containerParents, byte constraints, byte scrollable, byte layout, byte row, byte column, int containerId) {
		SerializerHelper.writeInt(MagicConstant.ADD_CONTAINER_TO_CONTAINER, buffer);
		SerializerHelper.writeInt(containerParents, buffer);
		SerializerHelper.writeByte(constraints, buffer);
		SerializerHelper.writeByte(scrollable, buffer);
		SerializerHelper.writeByte(layout, buffer);
		SerializerHelper.writeByte(row, buffer);
		SerializerHelper.writeByte(column, buffer);
		SerializerHelper.writeInt(containerId, buffer);
	}

	public static void addLabelToPage(IoBuffer buffer, int containerId, byte constraints, byte textPosition, byte alignment, byte[] image, byte borderStyle, String text,
			int color, byte startTicker, boolean isFocusable, int bgColor, byte topPadding, byte leftPadding, byte bottomPadding, byte rightPadding, byte topMargin,
			byte leftMargin, byte bottomMargin, byte rightMargin, int componentId) {
		SerializerHelper.writeInt(MagicConstant.ADD_COMPONENT_LABEL, buffer);
		SerializerHelper.writeInt(containerId, buffer);
		SerializerHelper.writeByte(constraints, buffer);
		SerializerHelper.writeByte(textPosition, buffer);
		SerializerHelper.writeByte(alignment, buffer);
		SerializerHelper.write(image, buffer);
		SerializerHelper.writeByte(borderStyle, buffer);
		SerializerHelper.writeString(text, buffer);
		SerializerHelper.writeInt(color, buffer);
		SerializerHelper.writeByte(startTicker, buffer);
		SerializerHelper.writeBoolean(isFocusable, buffer);
		SerializerHelper.writeInt(bgColor, buffer);
		SerializerHelper.writeByte(topPadding, buffer);
		SerializerHelper.writeByte(leftPadding, buffer);
		SerializerHelper.writeByte(bottomPadding, buffer);
		SerializerHelper.writeByte(rightPadding, buffer);
		SerializerHelper.writeByte(topMargin, buffer);
		SerializerHelper.writeByte(leftMargin, buffer);
		SerializerHelper.writeByte(bottomMargin, buffer);
		SerializerHelper.writeByte(rightMargin, buffer);
		SerializerHelper.writeInt(componentId, buffer);
	}

	public static void addButtonToPage(IoBuffer buffer, int containerId, byte constraints, byte textPosition, byte alignment, byte[] image, byte bigIcon, byte[] selectedImage,
			byte backgroundStyle, byte style, String text, int color, byte font, byte fontSize, byte fontStyle, boolean isFocusable, byte topPadding, byte leftPadding,
			byte bottomPadding, byte rightPadding, byte topMargin, byte leftMargin, byte bottomMargin, byte rightMargin, byte[] action, int componentId) {
		SerializerHelper.writeInt(MagicConstant.ADD_COMPONENT_BUTTON, buffer);
		SerializerHelper.writeInt(containerId, buffer);
		SerializerHelper.writeByte(constraints, buffer);
		SerializerHelper.writeByte(textPosition, buffer);
		SerializerHelper.writeByte(alignment, buffer);
		SerializerHelper.write(image, buffer);
		SerializerHelper.writeByte(bigIcon, buffer);
		SerializerHelper.write(selectedImage, buffer);
		SerializerHelper.writeByte(backgroundStyle, buffer);
		SerializerHelper.writeByte(style, buffer);
		SerializerHelper.writeString(text, buffer);
		SerializerHelper.writeInt(color, buffer);
		SerializerHelper.writeByte(font, buffer);
		SerializerHelper.writeByte(fontSize, buffer);
		SerializerHelper.writeByte(fontStyle, buffer);
		SerializerHelper.writeBoolean(isFocusable, buffer);
		SerializerHelper.writeByte(topPadding, buffer);
		SerializerHelper.writeByte(leftPadding, buffer);
		SerializerHelper.writeByte(bottomPadding, buffer);
		SerializerHelper.writeByte(rightPadding, buffer);
		SerializerHelper.writeByte(topMargin, buffer);
		SerializerHelper.writeByte(leftMargin, buffer);
		SerializerHelper.writeByte(bottomMargin, buffer);
		SerializerHelper.writeByte(rightMargin, buffer);
		SerializerHelper.write(action, buffer);
		SerializerHelper.writeInt(componentId, buffer);
	}

	public static void addLinkToPage(IoBuffer buffer, int containerId, byte constraints, String text, int color, byte[] action, int componentId) {
		SerializerHelper.writeInt(MagicConstant.ADD_COMPONENT_LINK, buffer);
		SerializerHelper.writeInt(containerId, buffer);
		SerializerHelper.writeByte(constraints, buffer);
		SerializerHelper.writeString(text, buffer);
		SerializerHelper.writeInt(color, buffer);
		SerializerHelper.write(action, 0, action.length, buffer);
		SerializerHelper.writeInt(componentId, buffer);
	}

	public static void addCheckBoxToPage(IoBuffer buffer, int containerId, byte constraints, byte textPosition, byte alignment, String text, int color, byte isChosen,
			int componentId) {
		SerializerHelper.writeInt(MagicConstant.ADD_COMPONENT_CHECKBOX, buffer);
		SerializerHelper.writeInt(containerId, buffer);
		SerializerHelper.writeByte(constraints, buffer);
		SerializerHelper.writeByte(textPosition, buffer);
		SerializerHelper.writeByte(alignment, buffer);
		SerializerHelper.writeString(text, buffer);
		SerializerHelper.writeInt(color, buffer);
		SerializerHelper.writeByte(isChosen, buffer);
		SerializerHelper.writeInt(componentId, buffer);
	}

	public static void addRadioButtonToPage(IoBuffer buffer, int containerId, byte constraints, byte textPosition, byte alignment, String[] items, int color, int componentId,
			int selected) {
		SerializerHelper.writeInt(MagicConstant.ADD_COMPONENT_RADIO_BUTTON_GROUP, buffer);
		SerializerHelper.writeInt(containerId, buffer);
		SerializerHelper.writeByte(constraints, buffer);
		SerializerHelper.writeByte(textPosition, buffer);
		SerializerHelper.writeByte(alignment, buffer);
		SerializerHelper.writeInt(color, buffer);
		SerializerHelper.writeInt(selected, buffer);
		int l = items.length;
		SerializerHelper.writeInt(l, buffer);
		for (int i = 0; i < l; i++) {
			SerializerHelper.writeString(items[i], buffer);
		}
		SerializerHelper.writeInt(componentId, buffer);
	}

	public static void addComboBoxToPage(IoBuffer buffer, int containerId, byte constraints, String[] items, int color, int selected, int componentId) {
		SerializerHelper.writeInt(MagicConstant.ADD_COMPONENT_COMBOBOX, buffer);
		SerializerHelper.writeInt(containerId, buffer);
		SerializerHelper.writeByte(constraints, buffer);
		SerializerHelper.writeInt(color, buffer);
		SerializerHelper.writeInt(selected, buffer);
		int l = items.length;
		SerializerHelper.writeInt(l, buffer);
		for (int i = 0; i < l; i++) {
			SerializerHelper.writeString(items[i], buffer);
		}
		SerializerHelper.writeInt(componentId, buffer);
	}

	public static void addListToPage(IoBuffer buffer, int containerId, byte constraints, byte typeOfRenderer, int color, List<String> items, List<String> subItems,
			byte[][] images, byte[] action, long[] constants, int componentId) {
		SerializerHelper.writeInt(MagicConstant.ADD_COMPONENT_LIST, buffer);
		SerializerHelper.writeInt(containerId, buffer);
		SerializerHelper.writeByte(constraints, buffer);
		SerializerHelper.writeByte(typeOfRenderer, buffer);
		SerializerHelper.writeInt(color, buffer);
		int l = items.size();
		SerializerHelper.writeInt(l, buffer);
		for (int i = 0; i < l; i++) {
			SerializerHelper.writeString(items.get(i), buffer);
		}
		if (subItems != null) {
			int k = subItems.size();
			SerializerHelper.writeInt(k, buffer);
			for (int i = 0; i < k; i++) {
				SerializerHelper.writeString(subItems.get(i), buffer);
			}
		} else {
			SerializerHelper.writeInt(0, buffer);
		}
		if (images != null) {
			int k = images.length;
			SerializerHelper.writeInt(k, buffer);
			for (int i = 0; i < k; i++) {
				SerializerHelper.write(images[i], buffer);
			}
		} else {
			SerializerHelper.writeInt(0, buffer);
		}
		if (constants != null) {
			int t = constants.length;
			SerializerHelper.writeInt(t, buffer);
			for (int i = 0; i < t; i++) {
				SerializerHelper.writeLong(constants[i], buffer);
			}
		} else {
			SerializerHelper.writeInt(0, buffer);
		}
		SerializerHelper.write(action, buffer);
		SerializerHelper.writeInt(componentId, buffer);
	}

	public static void addTextFieldToPage(IoBuffer buffer, int containerId, byte constraints, String text, int color, int typeOfInput, byte intputMode, int maxSize,
			boolean isEditable, boolean isFocusable, int componentId) {
		SerializerHelper.writeInt(MagicConstant.ADD_COMPONENT_TEXTFIELD, buffer);
		SerializerHelper.writeInt(containerId, buffer);
		SerializerHelper.writeByte(constraints, buffer);
		SerializerHelper.writeString(text, buffer);
		SerializerHelper.writeInt(color, buffer);
		SerializerHelper.writeInt(typeOfInput, buffer);
		SerializerHelper.writeByte(intputMode, buffer);
		SerializerHelper.writeInt(maxSize, buffer);
		SerializerHelper.writeBoolean(isEditable, buffer);
		SerializerHelper.writeBoolean(isFocusable, buffer);
		SerializerHelper.writeInt(componentId, buffer);
	}

	public static void addTextAreaToPage(IoBuffer buffer, int containerId, byte constraints, String text, int color, byte rowsGap, int typeOfInput, int maxSize,
			boolean isEditable, boolean isFocusable, int bgColor, byte topPadding, byte leftPadding, byte bottomPadding, byte rightPadding, byte topMargin, byte leftMargin,
			byte bottomMargin, byte rightMargin, int componentId) {
		SerializerHelper.writeInt(MagicConstant.ADD_COMPONENT_TEXTAREA, buffer);
		SerializerHelper.writeInt(containerId, buffer);
		SerializerHelper.writeByte(constraints, buffer);
		SerializerHelper.writeString(text, buffer);
		SerializerHelper.writeInt(color, buffer);
		SerializerHelper.writeByte(rowsGap, buffer);
		SerializerHelper.writeInt(typeOfInput, buffer);
		SerializerHelper.writeInt(maxSize, buffer);
		SerializerHelper.writeBoolean(isEditable, buffer);
		SerializerHelper.writeBoolean(isFocusable, buffer);
		SerializerHelper.writeInt(bgColor, buffer);
		SerializerHelper.writeByte(topPadding, buffer);
		SerializerHelper.writeByte(leftPadding, buffer);
		SerializerHelper.writeByte(bottomPadding, buffer);
		SerializerHelper.writeByte(rightPadding, buffer);
		SerializerHelper.writeByte(topMargin, buffer);
		SerializerHelper.writeByte(leftMargin, buffer);
		SerializerHelper.writeByte(bottomMargin, buffer);
		SerializerHelper.writeByte(rightMargin, buffer);
		SerializerHelper.writeInt(componentId, buffer);
	}

	// ============================ACTIONS============================
	private static void sendSMS(IoBuffer buffer, String content, String to) {
		SerializerHelper.writeInt(MagicConstant.SEND_SMS_COMMAND, buffer);
		SerializerHelper.writeString(content, buffer);
		SerializerHelper.writeString(to, buffer);
	}

	public static byte[] createActionSendSMS(String content, String to) {
		IoBuffer buffer = IoBuffer.allocate(4);
		try {
			buffer.setAutoExpand(true);
			sendSMS(buffer, content, to);
			buffer.flip();
			return buffer.array();
		} catch (Exception e) {
			log.error("", e);
		}
		return null;
	}

	private static void platformRequest(IoBuffer buffer, String url) {
		SerializerHelper.writeInt(MagicConstant.OPEN_URL_COMMAND, buffer);
		SerializerHelper.writeString(url, buffer);
	}

	public static byte[] createActionPlatformRequest(String url) {
		IoBuffer buffer = IoBuffer.allocate(4);
		try {
			buffer.setAutoExpand(true);
			platformRequest(buffer, url);
			buffer.flip();
			return buffer.array();
		} catch (Exception e) {
			log.error("", e);
		}
		return null;
	}

	private static void showDialogInfo(IoBuffer buffer, String title, String content) {
		SerializerHelper.writeInt(MagicConstant.SHOW_DIALOG_COMMAND, buffer);
		SerializerHelper.writeString(title, buffer);
		SerializerHelper.writeString(content, buffer);
	}

	public static byte[] createActionShowDialogInfo(String title, String content) {
		IoBuffer buffer = IoBuffer.allocate(4);
		try {
			buffer.setAutoExpand(true);
			showDialogInfo(buffer, title, content);
			buffer.flip();
			return buffer.array();
		} catch (Exception e) {
			log.error("", e);
		}
		return null;
	}

	private static void chatTo(IoBuffer buffer, String username, long userId) {
		SerializerHelper.writeInt(MagicConstant.CHAT_TO_COMMAND, buffer);
		SerializerHelper.writeString(username, buffer);
		SerializerHelper.writeLong(userId, buffer);
	}

	public static byte[] createActionChatTo(String username, long userId) {
		IoBuffer buffer = IoBuffer.allocate(4);
		try {
			buffer.setAutoExpand(true);
			chatTo(buffer, username, userId);
			buffer.flip();
			return buffer.array();
		} catch (Exception e) {
			log.error("", e);
		}
		return null;
	}

	private static void openConferenceInviteListPage(IoBuffer buffer) {
		SerializerHelper.writeInt(MagicConstant.CHAT_TO_COMMAND, buffer);
	}

	public static byte[] createActionOpenConferenceInviteListPage(String username, long userId) {
		IoBuffer buffer = IoBuffer.allocate(4);
		try {
			buffer.setAutoExpand(true);
			openConferenceInviteListPage(buffer);
			buffer.flip();
			return buffer.array();
		} catch (Exception e) {
			log.error("", e);
		}
		return null;
	}

	public static void closePage(IoBuffer buffer, int pageId, boolean isShow) {
		SerializerHelper.writeInt(MagicConstant.CLOSE_PAGE_COMMAND, buffer);
		SerializerHelper.writeInt(pageId, buffer);
		SerializerHelper.writeBoolean(isShow, buffer);
	}

	public static byte[] createActionClosePage(int pageId, boolean isShow) { // isShow = false: close currentPage and DO NOT show previous of currentPage
		IoBuffer buffer = IoBuffer.allocate(4);
		try {
			buffer.setAutoExpand(true);
			closePage(buffer, pageId, isShow);
			buffer.flip();
			return buffer.array();
		} catch (Exception e) {
			log.error("", e);
		}
		return null;
	}

	private static void exitApp(IoBuffer buffer) {
		SerializerHelper.writeInt(MagicConstant.EXIT_APP_COMMAND, buffer);
	}

	public static byte[] createActionExitApp() {
		IoBuffer buffer = IoBuffer.allocate(4);
		try {
			buffer.setAutoExpand(true);
			exitApp(buffer);
			buffer.flip();
			return buffer.array();
		} catch (Exception e) {
			log.error("", e);
		}
		return null;
	}

	private static void disconnect(IoBuffer buffer) {
		SerializerHelper.writeInt(MagicConstant.DISCONNECT_COMMAND, buffer);
	}

	public static byte[] createActionDisconnect() {
		IoBuffer buffer = IoBuffer.allocate(4);
		try {
			buffer.setAutoExpand(true);
			disconnect(buffer);
			buffer.flip();
			return buffer.array();
		} catch (Exception e) {
			log.error("", e);
		}
		return null;
	}

	private static void openBuddyPage(IoBuffer buffer) {
		SerializerHelper.writeInt(MagicConstant.OPEN_BUDDYPAGE_COMMAND, buffer);
	}

	public static byte[] createActionOpenBuddyPage() {
		IoBuffer buffer = IoBuffer.allocate(4);
		try {
			buffer.setAutoExpand(true);
			openBuddyPage(buffer);
			buffer.flip();
			return buffer.array();
		} catch (Exception e) {
			log.error("", e);
		}
		return null;
	}

	private static void openChatListPage(IoBuffer buffer) {
		SerializerHelper.writeInt(MagicConstant.OPEN_CHAT_LIST_COMMAND, buffer);
	}

	public static byte[] createActionOpenChatListPage() {
		IoBuffer buffer = IoBuffer.allocate(4);
		try {
			buffer.setAutoExpand(true);
			openChatListPage(buffer);
			buffer.flip();
			return buffer.array();
		} catch (Exception e) {
			log.error("", e);
		}
		return null;
	}

	private static void openConferenceListPage(IoBuffer buffer) {
		SerializerHelper.writeInt(MagicConstant.OPEN_CONFERENCE_LIST_COMMAND, buffer);
	}

	public static byte[] createActionOpenConferenceListPage() {
		IoBuffer buffer = IoBuffer.allocate(4);
		try {
			buffer.setAutoExpand(true);
			openConferenceListPage(buffer);
			buffer.flip();
			return buffer.array();
		} catch (Exception e) {
			log.error("", e);
		}
		return null;
	}

	private static void openInviteConferencePage(IoBuffer buffer, long conferenceId, boolean isStart) {
		SerializerHelper.writeInt(MagicConstant.OPEN_INVITE_CONFERENCE_COMMAND, buffer);
		SerializerHelper.writeLong(conferenceId, buffer);
		SerializerHelper.writeBoolean(isStart, buffer);
	}

	public static byte[] createActionOpenInviteConferencePage(long conferenceId, boolean isStart) {
		IoBuffer buffer = IoBuffer.allocate(4);
		try {
			buffer.setAutoExpand(true);
			openInviteConferencePage(buffer, conferenceId, isStart);
			buffer.flip();
			return buffer.array();
		} catch (Exception e) {
			log.error("", e);
		}
		return null;
	}

	private static void openConfirmJoinConferencePage(IoBuffer buffer) {
		SerializerHelper.writeInt(MagicConstant.OPEN_CONFIRM_JOIN_CONFERENCE_COMMAND, buffer);
	}

	public static byte[] createActionOpenConfirmJoinConferencePage() {
		IoBuffer buffer = IoBuffer.allocate(4);
		try {
			buffer.setAutoExpand(true);
			openConfirmJoinConferencePage(buffer);
			buffer.flip();
			return buffer.array();
		} catch (Exception e) {
			log.error("", e);
		}
		return null;
	}

	private static void usingCamera(IoBuffer buffer, long entityId, long albumId) {
		SerializerHelper.writeInt(MagicConstant.USING_CAMERA_COMMAND, buffer);
		SerializerHelper.writeLong(entityId, buffer);
		SerializerHelper.writeLong(albumId, buffer);
	}

	public static byte[] createActionUsingCamera(long entityId, long albumId) {
		IoBuffer buffer = IoBuffer.allocate(4);
		try {
			buffer.setAutoExpand(true);
			usingCamera(buffer, entityId, albumId);
			buffer.flip();
			return buffer.array();
		} catch (Exception e) {
			log.error("", e);
		}
		return null;
	}

	private static void usingBrowsingCard(IoBuffer buffer, long entityId, long albumId) {
		SerializerHelper.writeInt(MagicConstant.BROWSING_CARD_COMMAND, buffer);
		SerializerHelper.writeLong(entityId, buffer);
		SerializerHelper.writeLong(albumId, buffer);
	}

	public static byte[] createActionBrowsingCard(long entityId, long albumId) {
		IoBuffer buffer = IoBuffer.allocate(4);
		try {
			buffer.setAutoExpand(true);
			usingBrowsingCard(buffer, entityId, albumId);
			buffer.flip();
			return buffer.array();
		} catch (Exception e) {
			log.error("", e);
		}
		return null;
	}

	private static void openPlayerPage(IoBuffer buffer, int songId) {
		SerializerHelper.writeInt(MagicConstant.OPEN_PLAYER_PAGE_COMMAND, buffer);
		SerializerHelper.writeInt(songId, buffer);
	}

	public static byte[] createActionOpenPlayerPage(int songId) {
		IoBuffer buffer = IoBuffer.allocate(4);
		try {
			buffer.setAutoExpand(true);
			openPlayerPage(buffer, songId);
			buffer.flip();
			return buffer.array();
		} catch (Exception e) {
			log.error("", e);
		}
		return null;
	}

	public static void endPage(IoBuffer buffer) {
		SerializerHelper.writeInt(MagicConstant.END_PAGE, buffer);
	}

	public static void endCommand(IoBuffer buffer) {
		SerializerHelper.writeInt(MagicConstant.END_COMMAND, buffer);
	}

	public static void requestFocus(IoBuffer buffer, int compId) {
		SerializerHelper.writeInt(MagicConstant.REQUEST_FOCUS_COMMAND, buffer);
		SerializerHelper.writeInt(compId, buffer);
	}

	private static void startVibrate(IoBuffer buffer) {
		SerializerHelper.writeInt(MagicConstant.START_VIBRATE_COMMAND, buffer);
	}

	public static byte[] createActionStartVibrate() {
		IoBuffer buffer = IoBuffer.allocate(4);
		try {
			buffer.setAutoExpand(true);
			startVibrate(buffer);
			buffer.flip();
			return buffer.array();
		} catch (Exception e) {
			log.error("", e);
		}
		return null;
	}

	private static void openUrl(IoBuffer buffer, String url) {
		SerializerHelper.writeInt(MagicConstant.OPEN_URL_TO_BROWSER_COMMAND, buffer);
		SerializerHelper.writeString(url, buffer);
	}

	public static byte[] createActionOpenUrl(String url) {
		IoBuffer buffer = IoBuffer.allocate(4);
		try {
			buffer.setAutoExpand(true);
			openUrl(buffer, url);
			buffer.flip();
			return buffer.array();
		} catch (Exception e) {
			log.error("", e);
		}
		return null;
	}
}
